import codecs
import json
import logging
import time
import re
import emoji
from datetime import datetime
from pprint import pprint
from tqdm import tqdm
import uuid
import requests

from django.views import View
from django.http.request import HttpRequest
from django.http import JsonResponse
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.db.models import Q
from django.utils import timezone

from .models import CuZuModel as CuZuModel_Class
from .utils import idmi_nlp_tool

CuZuModel = CuZuModel_Class()


sourceId_list = [
    "autohome_public_praise",
    "dongchedi_public_praise",
]


def generate_unique_id():
    unique_id = uuid.uuid4().hex
    return unique_id


class StoreCuzu(View):
    '''
        store the cuzu sent
    {
        "id": 67369,
        "sourceId": "dongchedi_public_praise",
        "url_temp": "7087644106989506592",
        "price": "8.60万",
        "boughtdate": "2018-12-01",
        "address": "茂名",
        "create_time": "2018-12-01",
        "image_url": "",
        "brand": "Toyota",
        "series": "Corolla ",
        "carspec_name": "1.8L CVT GLX-i",
        "title": "",
        "customer_voice": "人生第一部车 ，不算很完美 ，也没很差劲，优点是:毛病少省油动力还过得去。缺点是:对我而言空间小了些，车漆非常薄，指甲不小心碰下都有微妙的划痕",
        "URL": "https://www.dongchedi.com/koubei/7087644106989506592",
    }
    '''

    def post(self, request: HttpRequest) -> JsonResponse:
        sourceId = request.POST.get('sourceId')
        if sourceId in sourceId_list:
            CuZuModel.uuid = generate_unique_id()
            CuZuModel.create_time = request.POST.get('create_time')
            CuZuModel.sourceId = sourceId
            CuZuModel.update_time = request.POST.get('create_time')
            CuZuModel.url_temp = request.POST.get('url_temp')
            CuZuModel.price = request.POST.get('price')
            CuZuModel.boughtdate = request.POST.get('boughtdate')
            CuZuModel.address = request.POST.get('address')
            CuZuModel.image_url = request.POST.get('image_url')
            CuZuModel.brand = request.POST.get('brand')
            CuZuModel.series = request.POST.get('series')
            CuZuModel.carspec_name = request.POST.get('carspec_name')
            title = request.POST.get('title')
            customer_voice = request.POST.get('customer_voice')
            CuZuModel.title = title
            CuZuModel.customer_voice = customer_voice

            if len(title):
                title_en = idmi_nlp_tool._translate_util_zh2en(sentence=title)['en']
            else:
                title_en = ''
            if len(customer_voice):
                customer_voice_en = idmi_nlp_tool._translate_util_zh2en(sentence=customer_voice)['en']
            else:
                customer_voice_en = ''
            CuZuModel.customer_voice_en = customer_voice_en
            CuZuModel.title_en = title_en

            if len(customer_voice):
                zh, en = idmi_nlp_tool.qwen.csp(customer_voice=customer_voice)
                if len(zh):
                    CuZuModel.llm_csp = json.dumps(zh, ensure_ascii=False)
                    CuZuModel.llm_csp_en = json.dumps(en, ensure_ascii=False)
                else:
                    CuZuModel.llm_csp = ''
                    CuZuModel.llm_csp_en = ''
                zh, en = idmi_nlp_tool.qwen.feel(customer_voice=customer_voice)
                if len(zh):
                    CuZuModel.llm_feeling = json.dumps(zh, ensure_ascii=False)
                    CuZuModel.llm_feeling_en = json.dumps(en, ensure_ascii=False)
                else:
                    CuZuModel.llm_feeling = ''
                    CuZuModel.llm_feeling_en = ''
            else:
                CuZuModel.llm_csp = ''
                CuZuModel.llm_csp_en = ''
                CuZuModel.llm_feeling = ''
                CuZuModel.llm_feeling_en = ''

            CuZuModel.URL = request.POST.get('URL')
            CuZuModel.save(force_insert=True)
            return JsonResponse({ 'code' : 200 })
        else:
            return JsonResponse({ 'code' : 500 })


    def clean_sentence(self, sentence):
        if len(sentence):
            sentence = sentence. \
                replace('\r', ''). \
                replace('\n', ''). \
                replace('\t', ''). \
                replace('"', ''). \
                replace("'", ""). \
                replace('“', ' '). \
                replace('”', ' ')
            try:
                sentence = emoji.demojize(sentence)
            except:
                sentence = sentence
            return sentence
        else:
            return ''

with codecs.open(filename='temp/01.json',
                 mode='r', encoding='utf-8') as fr:
    DICT = json.load(fr)


class UpdateCuZu(View):

    def post(self, request):
        # _res_list = CuZuModel_Class.objects.filter().all().values(
        #     'uuid',
        #     'brand',
        #     'series',
        #     'llm_feeling_en',
        #     'llm_feeling',
        #     'llm_csp',
        #     'llm_csp_en'
        # )
        # error_amount = 0
        # print(len(_res_list))
        # for r in tqdm(_res_list):
        #     uuid_str = r['uuid']
        #     try:
        #         llm_feeling_en = json.loads(r['llm_feeling_en'])
        #         llm_feeling = json.loads(r['llm_feeling'])
        #         llm_csp = json.loads(r['llm_csp'])
        #         llm_csp_en = json.loads(r['llm_csp_en'])
        #
        #         # 接下来，看一下 llm_feeling_en 中标签的错误相关数据
        #         for index in range(len(llm_feeling)):
        #             llm_zh = llm_feeling[index]
        #             llm_en = llm_feeling_en[index]
        #
        #             feel_1_zh = llm_zh['feel_1']
        #             feel_2_zh = llm_zh['feel_2']
        #
        #             feel_1_en = llm_en['feel_1']
        #             feel_2_en = llm_en['feel_2']
        #             content_list_en = llm_en['content_list']
        #             sentiment_en = llm_en['sentiment']
        #
        #             _feel_1_en = DICT[feel_1_zh]
        #             _feel_2_en = DICT[feel_2_zh]
        #
        #             if feel_1_en != _feel_1_en:
        #                 error_amount += 1
        #             else:
        #                 ...
        #             if feel_2_en != _feel_2_en:
        #                 error_amount += 1
        #             else:
        #                 ...
        #
        #     except:
        #         error_amount += 1
        #         continue
        # print(error_amount)
        return JsonResponse({'status': 200})




class UpdateCuZuContentList(View):

    def clean_sentence(self, customer_voice):
        customer_voice = customer_voice. \
            replace('\r', ''). \
            replace('\n', ''). \
            replace('\t', ''). \
            replace('"', ''). \
            replace("'", ""). \
            replace('  ', ""). \
            replace('“', ' '). \
            replace('”', ' ')
        return customer_voice

    def translate_sent(self, sentence):
        sentence = self.clean_sentence(sentence)
        data = {
            'sentence': sentence,
            'password': 'liuyang177',
            'src_language_type': 'zh',
            'tar_language_type': 'en'
        }
        res = requests.post(
            url='http://152.32.135.145:17300/translate',
            data=data
        )
        return json.loads(res.content.decode('unicode_escape'))['en']



    # def post(self, request):
    #     # return JsonResponse({'status': 200})

    def post(self, request):
        # password = request.POST.get('password')
        # days = int(request.POST.get('days'))
        #
        # if password == 'liuyang177':
        #     end_date = timezone.now()
        #     start_date = end_date - timezone.timedelta(hours=24 * days,
        #                                                minutes=0, seconds=0)
        #     _res_list = CuZuModel_Class.objects.filter(
        #         Q(create_time__range=(start_date, end_date))
        #     ).all().values(
        #         'uuid',
        #         'brand',
        #         'series',
        #         'llm_feeling_en',
        #         'llm_feeling'
        #     )
        #     for _res in tqdm(_res_list):
        #         uuid_str = _res['uuid']
        #         brand = _res['brand']
        #         series = _res['series']
        #         try:
        #             llm_feeling = json.loads(_res['llm_feeling'])
        #         except:
        #             continue
        #
        #         llm_feeling_en = []
        #         if series :#in ['ID.3', 'ID.4 CROZZ', 'ID.6 CROZZ', 'ID.4 X', 'ID.6 X', 'ID.7 VIZZION', 'ID.UNYX']:
        #             for index in range(len(llm_feeling)):
        #                 llm_en = {}
        #
        #                 llm_zh = llm_feeling[index]
        #                 feel_1_zh = llm_zh['feel_1']
        #                 feel_2_zh = llm_zh['feel_2']
        #                 sentiment_zh = llm_zh['sentiment']
        #
        #                 feel_1_en = DICT[feel_1_zh]
        #                 feel_2_en = DICT[feel_2_zh]
        #                 sentiment_en = SENTIMENT_DICT[sentiment_zh]
        #
        #                 content_list_zh = llm_zh['content_list']
        #                 content_list_en = []
        #                 for content_index in range(len(content_list_zh)):
        #                     content_zh = content_list_zh[content_index]
        #                     if content_zh in trans_log.keys():
        #                         content_en = trans_log[content_zh]
        #                     else:
        #                         content_en = self.translate_sent(content_zh)
        #                     # content_en = self.translate_sent(content_zh)
        #                     content_list_en.append(content_en)
        #
        #                 llm_en['content_list'] = content_list_en
        #                 llm_en['feel_1'] = feel_1_en
        #                 llm_en['feel_2'] = feel_2_en
        #                 llm_en['sentiment'] = sentiment_en
        #                 llm_feeling_en.append(llm_en)
        #         new_llm_feeling_en = json.dumps(llm_feeling_en, ensure_ascii=False)
        #         CuZuModel_Class.objects.filter(uuid=uuid_str).update(
        #             llm_feeling_en=new_llm_feeling_en,
        #             update_time=timezone.now()
        #         )
        # else:
        #     ...
        return JsonResponse({'status': 200})


#
# class UpdateCuZu(View):
#     def get(self, request):
        # _res_list = CuZuModel_Class.objects.all().values(
        #     'uuid',
        #     'brand',
        #     'series',
        #     'llm_feeling_en',
        #     'create_time'
        # )
        # print(len(_res_list))
        # with codecs.open(filename='/root/data/00_saved/20241219/04-trans.json',
        #                  mode='r', encoding='utf-8') as fr:
        #     DICT = json.load(fr)
        # for _res in tqdm(_res_list):
        #     try:
        #         uuid_str = _res['uuid']
        #         brand = _res['brand']
        #         series = _res['series']
        #         _llm_feeling_en = _res['llm_feeling_en']
        #         if  series in ['ID.3', 'ID.4 CROZZ', 'ID.6 CROZZ', 'ID.4 X', 'ID.6 X', 'ID.7 VIZZION', 'ID.UNYX']:
        #             llm_feeling_en = []
        #             _llm_feeling_en = json.loads(_llm_feeling_en)
        #             for _d in _llm_feeling_en:
        #                 _content_list = _d['content_list']
        #                 content_list = []
        #                 feel_1 = _d['feel_1']
        #                 feel_2 = _d['feel_2']
        #                 sentiment = _d['sentiment']
        #                 for _content in _content_list:
        #                     _content_en = DICT.get(_content, '')
        #                     if len(_content_en):
        #                         content_list.append(_content_en)
        #                     else:
        #                         content_list.append(_content)
        #
        #                     llm_feeling_en.append({
        #                         'content_list': content_list,
        #                         'feel_1': feel_1,
        #                         'feel_2': feel_2,
        #                         'sentiment': sentiment
        #                     })
        #             llm_feeling_en = json.dumps(llm_feeling_en, ensure_ascii=False)
        #             CuZuModel_Class.objects.filter(uuid=uuid_str).update(
        #                 llm_feeling_en=llm_feeling_en,
        #                 update_time=timezone.now()
        #             )
        #             print(series)
        #             print(uuid_str)
        #     except:
        #         ...
        # return JsonResponse({'status': 200})

'''
class UpdateCuZu(View):
    def post(self, request):
        uuid_str = request.POST.get('uuid')
        create_time = request.POST.get('create_time')
        update_time = create_time
        sourceId = request.POST.get('sourceId')
        url_temp = request.POST.get('url_temp')
        price = request.POST.get('price')
        boughtdate = request.POST.get('boughtdate')
        address = request.POST.get('address')
        image_url = request.POST.get('image_url')
        brand = request.POST.get('brand')
        series = request.POST.get('series')
        carspec_name = request.POST.get('carspec_name')
        title = request.POST.get('title')
        customer_voice = request.POST.get('customer_voice')
        title_en = request.POST.get('title_en')
        customer_voice_en = request.POST.get('customer_voice_en')
        URL = request.POST.get('URL')


        llm_csp = json.loads(request.POST.get('llm_csp'))
        llm_csp_en = json.loads(request.POST.get('llm_csp_en'))
        llm_feeling = json.loads(request.POST.get('llm_feeling'))
        llm_feeling_en = json.loads(request.POST.get('llm_feeling_en'))




        CuZuModel.uuid = uuid_str
        CuZuModel.create_time = create_time
        CuZuModel.sourceId = sourceId
        CuZuModel.update_time = update_time
        CuZuModel.url_temp = url_temp
        CuZuModel.price = price
        CuZuModel.boughtdate = boughtdate
        CuZuModel.address = address
        CuZuModel.image_url = image_url
        CuZuModel.brand = brand
        CuZuModel.series = series
        CuZuModel.carspec_name = carspec_name
        CuZuModel.title = title
        CuZuModel.title_en = title_en
        CuZuModel.customer_voice = customer_voice
        CuZuModel.customer_voice_en = customer_voice_en
        CuZuModel.URL = URL
        CuZuModel.llm_quality = json.dumps([], ensure_ascii=False)
        CuZuModel.llm_csp = json.dumps(llm_csp, ensure_ascii=False)
        CuZuModel.llm_feeling = json.dumps(llm_feeling, ensure_ascii=False)
        CuZuModel.llm_quality_en = json.dumps([], ensure_ascii=False)
        CuZuModel.llm_csp_en = json.dumps(llm_csp_en, ensure_ascii=False)
        CuZuModel.llm_feeling_en = json.dumps(llm_feeling_en, ensure_ascii=False)
        CuZuModel.save()

        return JsonResponse({ 'code' : 200})

'''

'''
之前存留的翻译CuZu英文的数据


with codecs.open(filename='/root/03_backend/idmi-backend-v5/idmi_backend/sales-zh2en.json',
                 mode='r', encoding='utf-8') as fr:
    zh2en_dict = json.load(fr)
class UpdateCuZu(View):

    def post(self, request):
        line = request.POST.get('line')
        line = json.loads(line)
        # dict_keys(['id', 'sourceId', 'url_temp', 'price',
        #  'boughtdate', 'address', 'create_time', 'image_url',
        #  'brand', 'series', 'carspec_name',
        #  'title', 'customer_voice', 'URL',
        #  'title_en', 'customer_voice_en', 'llm_quality',
        #  'llm_csp', 'llm_feeling', 'llm_quality_en',
        #   'llm_csp_en', 'llm_feeling_en'])
        sourceId = line['sourceId']
        url_temp = line['url_temp']
        price = line['price']
        boughtdate = line['boughtdate']
        address = line['address']
        create_time = line['create_time']
        image_url = line['image_url']
        brand = line['brand']
        series = line['series']
        carspec_name = line['carspec_name']
        title = line['title']
        customer_voice = line['customer_voice']
        URL = line['URL']
        title_en = line['title_en']
        customer_voice_en = line['customer_voice_en']
        llm_quality = line['llm_quality']
        llm_csp = line['llm_csp']
        llm_feeling = line['llm_feeling']
        llm_quality_en = line['llm_quality_en']
        llm_csp_en = line['llm_csp_en']
        llm_feeling_en = line['llm_feeling_en']

        llm_csp_convert = []
        llm_feeling_convert = []
        llm_csp_en_convert = []
        llm_feeling_en_convert = []

        if isinstance(llm_feeling, str):
            if len(llm_feeling) > 0:
                llm_feeling = json.loads(llm_feeling[1:-1])
                for feeling in llm_feeling:
                    feel_1 = feeling['feel_1']
                    feel_2 = feeling['feel_2']
                    sentiment = feeling['sentiment']
                    content_list = feeling['content_list']
                    feel_1_en = zh2en_dict[feel_1]
                    feel_2_en = zh2en_dict[feel_2]
                    if sentiment == '正向情感':
                        sentiment_en = 'positive'
                    elif sentiment == '负向情感':
                        sentiment_en = 'negative'
                    elif sentiment == '中性情感':
                        sentiment_en = 'neutral'
                    else:
                        raise RuntimeError('sentiment error')

                    llm_feeling_convert.append({
                        'feel_1': feel_1,
                        'feel_2': feel_2,
                        'content_list': content_list,
                        'sentiment': sentiment,
                    })
                    llm_feeling_en_convert.append({
                        'feel_1': feel_1_en,
                        'feel_2': feel_2_en,
                        'content_list': content_list,
                        'sentiment': sentiment_en,
                    })
        if isinstance(llm_csp, str):
            if len(llm_csp) > 0:
                llm_csp = json.loads(llm_csp[1:-1])
                for csp in llm_csp:
                    csp_1 = csp['csp_1']
                    csp_2 = csp['csp_2']
                    csp_3 = csp['csp_3']
                    sentiment = csp['sentiment']
                    content_list = csp['content_list']
                    csp_1_en = zh2en_dict[csp_1]
                    csp_2_en = zh2en_dict[csp_2]
                    csp_3_en = zh2en_dict[csp_3]
                    if sentiment == '正向情感':
                        sentiment_en = 'positive'
                    elif sentiment == '负向情感':
                        sentiment_en = 'negative'
                    elif sentiment == '中性情感':
                        sentiment_en = 'neutral'
                    else:
                        raise RuntimeError('sentiment error')

                    llm_csp_convert.append({
                        'csp_1': csp_1,
                        'csp_2': csp_2,
                        'csp_3': csp_3,
                        'content_list': content_list,
                        'sentiment': sentiment,
                    })
                    llm_csp_en_convert.append({
                        'csp_1': csp_1_en,
                        'csp_2': csp_2_en,
                        'csp_3': csp_3_en,
                        'content_list': content_list,
                        'sentiment': sentiment_en,
                    })
        CuZuModel.uuid = generate_unique_id()
        CuZuModel.sourceId = sourceId
        CuZuModel.url_temp = url_temp
        CuZuModel.price = price
        CuZuModel.boughtdate = boughtdate
        CuZuModel.address = address
        CuZuModel.create_time = timezone.now()
        CuZuModel.update_time = timezone.now()
        CuZuModel.image_url = image_url
        CuZuModel.brand = brand
        CuZuModel.series = series
        CuZuModel.carspec_name = carspec_name
        CuZuModel.title = title
        CuZuModel.customer_voice = customer_voice
        CuZuModel.URL = URL
        CuZuModel.title_en = title_en
        CuZuModel.customer_voice_en = customer_voice_en
        CuZuModel.llm_quality = llm_quality
        CuZuModel.llm_csp = json.dumps(llm_csp_convert, ensure_ascii=False)
        CuZuModel.llm_feeling = json.dumps(llm_feeling_convert, ensure_ascii=False)
        CuZuModel.llm_quality_en = llm_quality_en
        CuZuModel.llm_csp_en = json.dumps(llm_csp_en_convert, ensure_ascii=False)
        CuZuModel.llm_feeling_en = json.dumps(llm_feeling_en_convert, ensure_ascii=False)
        CuZuModel.save(force_insert=True)

        return JsonResponse({'status': 200})
'''




